jQuery(function(event) {

	$('#locationsList input[type="button"].butDelete').click(function(e){
		var tr = $(this).parents('tr');
		var td = tr.children();
		var tdn = td.eq(1);
		if(confirm('Are you really want to delete city "' + tdn.text().replace('\'', '\\\'') + '"?'))
		{
			tr.find('input').attr('disabled', 'disabled');
			$.ajax({
				url: '/a_admin/location/translation.deleteSystemCity',
				method: 'post',
				dataType: 'json',
				timeout: 20000,
				async: true,
				data: {
					cityId: td.eq(0).text()
				},
				success: function(data){
					if(data.status == 'success')
					{
						tr.remove();
					}
					else
					{
						var message = 'Error!';
						if(data.message != undefined)
						{
							message = data.message; 
						}
						alert(message);
						tr.find('input').removeAttr('disabled');
					}
				},
				error: function(){
					alert('Error!');
					tr.find('input').removeAttr('disabled');
				}
			});
		}
	});

	$('#locationsList input[type="button"].butChange').click(function(e){
		var td = $(this).parents('tr').children();
		var tdt = td.eq(2);
		var tdb = td.eq(3);

		var val = tdt.text();
		tdt.html('');

		var inp = $('<input type="text" />');
		inp.val(val);
		inp.attr('title', val);
		tdt.append(inp);

		var butEdit = $(this);
		butEdit.hide();

		var funCloseEditing = function(e){
			tdb.find('input[type="button"]').not('.butChange').remove();
			butEdit.show();
		};

		$('<input type="button" value="ok" />').click(function(e){
			var newVal = inp.val().replace(/^[\s\t\r\n]+/g, '').replace(/[\s\t\r\n]+$/g, '');
			if(newVal.length > 0)
			{
				$.ajax({
					url: '/a_admin/location/translation.saveCityTranslation',
					method: 'post',
					timeout: 20000,
					async: true,
					dataType: 'json',
					data: {
						cityName: td.eq(1).text(),
						cityNameTranslation: newVal
					},
					success: function(data){
						if(data && data.status == 'success')
						{
							inp.remove();
							tdt.text(newVal);
							tdt.removeAttr('style');
							funCloseEditing();
						}
						else
						{
							alert('Translation saving error!');
						}
					},
					error: function(){
						alert('Translation saving error!');
					}
				});
			}
			else
			{
				alert('Please define a translation before saving the city.');
				inp.focus();
			}
		}).appendTo(tdb);

		$('<input type="button" value="cancel" />').click(function(){
			var val = inp.attr('title');
			inp.remove();
			tdt.text(val);
			funCloseEditing();
		}).appendTo(tdb);

	});

	$('#locationsList input[type="checkbox"].butRate').click(function(e){
		var but = $(this);

		var funRevert = function(e){
 			if(e.attr('checked'))
			{
				e.removeAttr('checked');
				e.next('a').find('img').removeClass('checked');
			}
			else
			{
				e.next('a').find('img').addClass('checked');
				e.attr('checked', 'checked');
			}
		};
		$.ajax({
			url: '/a_admin/location/translation.changeSystemCityRate',
			method: 'post',
			timeout: 20000,
			dataType: 'json',
			async: false,
			data: {
				cityId: but.val(),
				rated: (but.attr('checked') ? 'Y' : 'N')
			},
			success: function(data){
				if(data.status != undefined)
				{
					if(data.status != 'success')
					{
						alert('Rate changing error: ' + data.message);
						funRevert(but);
					}
				}
				else
				{
					alert('Rate changing error!');
					funRevert(but);
				}
			},
			error: function(){
				alert('Rate changing error!');
				funRevert(but);
			}
		});

	});

	$('#airportsList input[type="button"].butChange').click(function(e){
		var td = $(this).parents('tr').children();
		var tdt = td.eq(2);
		var tdb = td.eq(3);

		var val = tdt.text();
		tdt.html('');

		var inp = $('<input type="text" />');
		inp.val(val);
		inp.attr('title', val);
		tdt.append(inp);

		var butEdit = $(this);
		butEdit.hide();

		var funCloseEditing = function(e){
			tdb.find('input[type="button"]').not('.butChange').remove();
			butEdit.show();
		};

		$('<input type="button" value="ok" />').click(function(e){
			var newVal = inp.val().replace(/^[\s\t\r\n]+/g, '').replace(/[\s\t\r\n]+$/g, '');
			if(newVal.length > 0)
			{
				$.ajax({
					url: '/a_admin/location/translation.saveAirportTranslation',
					method: 'post',
					timeout: 20000,
					async: true,
					dataType: 'json',
					data: {
						airportName: td.eq(1).text(),
						airportNameTranslation: newVal
					},
					success: function(data){
						if(data && data.status == 'success')
						{
							inp.remove();
							tdt.text(newVal);
							tdt.removeAttr('style');
							funCloseEditing();
						}
						else
						{
							alert('Translation saving error!');
						}
					},
					error: function(){
						alert('Translation saving error!');
					}
				});
			}
			else
			{
				alert('Please define a translation before saving the airport.');
				inp.focus();
			}
		}).appendTo(tdb);

		$('<input type="button" value="cancel" />').click(function(){
			var val = inp.attr('title');
			inp.remove();
			tdt.text(val);
			funCloseEditing();
		}).appendTo(tdb);

	});

	$('#locationsList input[type="button"].butRegion').click(function(e){
		var butRegion = $(this);
		butRegion.hide();
		var tdb = butRegion.parent();
		var tds = tdb.parent().children();
		var tdr = tds.eq(7);
		var region = tdr.text();
		var regionId = tds.eq(6).text();
		var selectRegCities = $('#allCitiesSelect').clone().removeClass().css('width', '150px');
		var cityId = tds.eq(0).text();
		selectRegCities.val(regionId);
		selectRegCities.find('option[value="' + cityId + '"]').remove();
		tdr.text('');
		selectRegCities.appendTo(tdr).show();

		var funCloseRegion = function(e){
			tdb.children().not('.butRegion').remove();
			butRegion.show();
		};
		
		$('<input class="button-ok" type="button" value="' + 'BTN_OK'.translate() + '" />').click(function(e){
			var newregion = selectRegCities.val();
			$.ajax({
				url: '/a_admin/location/translation.setRegion',
				method: 'post',
				timeout: 20000,
				async: false,
				dataType: 'json',
				data: {
					cityId: cityId,
					regionCityId: newregion
				},
				success: function(data){
					if(data != undefined && data.status != undefined && data.status == 'success')
					{
						funCloseRegion();
						if(data.data.regionId == 0)
						{
							tds.eq(6).html('&nbsp;-&nbsp;');
							tdr.html('&nbsp;-&nbsp;');
						}
						else
						{
							tds.eq(6).text(data.data.regionId);
							tdr.text(data.data.regionName + ' (' + data.data.regionTranslation + ')');
						}
					}
					else
					{
						alert('Region saving error!');
					}							
				},
				error: function(){
					alert('Region saving error!');
				}
			});
		}).appendTo(tdb);
		
		$('<input class="button-cancel" type="button" value="' + 'BTN_CANCEL'.translate() + '" />').click(function(){
			tdr.text(region);
			funCloseRegion();
		}).appendTo(tdb);
		
	});
	
});
